import { Component, OnInit } from '@angular/core';
import { ClassService } from '../services/class.service';
import { Search, ClassEntity } from '../components/Interfaces/ClassEntity';
import { Term } from '../components/Interfaces/Term';
import { TermService } from '../services/term.service';

@Component({
  selector: 'app-show',
  templateUrl: './show.component.html',
  styleUrls: ['./show.component.css']
})
export class ShowComponent implements OnInit {


    search: Search = new Search();
    data: any[]; // 数据

    terms: Term[];
    selectedTerm: Term;
    MName: string;

    constructor(
        private classService: ClassService,
        private termService: TermService
    ) { }

    ngOnInit() {
        this.pageInit();
        this.MName = '上午';
    }

    /**
     *  页面初始化
     */
    pageInit() {
        this.getAllTerms();
    }

    getAllTerms() {
        this.termService.getTerms().then(res => {
            this.terms = res as Term[];
            this.initSeach();
            this.getClassEntity();
        });
    }

    initSeach() {
        // console.log(this.terms);
        this.selectedTerm = this.terms[0];
        this.search.term_id = this.terms[0].id;
        this.search.search_type = 1; // 默认上午
        this.search.all = true;
        // this.search.group_by = true;
    }

    getClassEntity() {
        this.classService.getClassEntitiesByCondition(this.search).subscribe(
            res => {
                const arr = new Array();
                // tslint:disable-next-line:forin
                for (const p in res.data ) {
                    const item = {
                        'title': p,
                        'list': res.data[p]
                    };
                    arr.push(item);
                }
                this.data = arr;
            }
        );
    }

    pickAM() {
        this.search.search_type = 1;
        this.MName = '上午';
        this.getClassEntity();
    }

    pickPM() {
        this.search.search_type = 2;
        this.MName = '下午';
        this.getClassEntity();
    }

    pickAN() {
        this.search.search_type = 3;
        this.MName = '晚上';
        this.getClassEntity();
    }

    searchClassEntity() {
        this.getClassEntity();
    }



}
